# 创建基类
from datetime import datetime

from sqlalchemy import Column, Integer, String, DateTime, Float
from sqlalchemy.orm import declarative_base

Base = declarative_base()

# Create your models here.

#发票主表
class Invoice(Base):
    __tablename__ = 'invoice'
    '''
    id,主键,自增
    invoice_type,发票类型
    invoice_code,发票编号
    issue_date,发票日期
    buyer_name,购买方名称
    buyer_tax_id,购买方税号
    buyer_address_phone,购买方地址电话
    buyer_bank_account,购买方银行账号
    seller_name,销售方名称
    seller_tax_id,销售方税号
    seller_address_phone,销售方地址电话
    seller_bank_account,销售方银行账号
    check_code,校验码
    machine_code,机器码
    total_amount,发票金额
    total_tax,发票税额
    total_price_tax,发票含税金额
    drawer,开票人
    created_at,创建时间
    updated_at
    '''
    id = Column(Integer, primary_key=True, autoincrement=True)
    invoice_type = Column(String(20))
    invoice_code = Column(String(20))
    issue_date = Column(DateTime)
    buyer_name = Column(String(100))
    buyer_tax_id = Column(String(20))
    buyer_address_phone = Column(String(20))
    buyer_bank_account = Column(String(20))
    seller_name = Column(String(100))
    seller_tax_id = Column(String(20))
    seller_address_phone = Column(String(20))
    seller_bank_account = Column(String(20))
    check_code = Column(String(20))
    machine_code = Column(String(20))
    total_amount = Column(Float)
    total_tax = Column(Float)
    total_price_tax = Column(Float)
    drawer = Column(String(100))
    # 日期填充默认值
    created_at = Column(DateTime, default=datetime.now)
    updated_at = Column(DateTime, default=datetime.now, onupdate=datetime.now)
    dify_user_id = Column(String(64))
    file_path = Column(String(255))
    #定义打印方法
    def __repr__(self):
        return (f"<Invoice(id={self.id}, "
                f"invoice_type='{self.invoice_type}', "
                f"invoice_code='{self.invoice_code}', "
                f"issue_date={self.issue_date}, "
                f"buyer_name='{self.buyer_name}', "
                f"buyer_tax_id='{self.buyer_tax_id}', "
                f"buyer_address_phone='{self.buyer_address_phone}', "
                f"buyer_bank_account='{self.buyer_bank_account}', "
                f"seller_name='{self.seller_name}', "
                f"seller_tax_id='{self.seller_tax_id}', "
                f"seller_address_phone='{self.seller_address_phone}', "
                f"seller_bank_account='{self.seller_bank_account}', "
                f"check_code='{self.check_code}', "
                f"machine_code='{self.machine_code}', "
                f"total_amount={self.total_amount}, "
                f"total_tax={self.total_tax}, "
                f"total_price_tax={self.total_price_tax}, "
                f"drawer='{self.drawer}', "
                f"created_at={self.created_at}, "
                f"updated_at={self.updated_at}, "
                f"dify_user_id='{self.dify_user_id}')")



#发票明细表
class InvoiceItem(Base):
    __tablename__ = 'invoice_item'
    '''
    id,主键,自增
    invoice_id,发票主表id
    item_name,商品名称
    specification,商品规格
    unit,商品单位
    quantity,商品数量
    unit_price,商品单价
    amount,商品金额
    tax_rate,商品税率
    tax_amount,商品税额
    created_at,创建时间
    updated_at
    '''
    id = Column(Integer, primary_key=True, autoincrement=True)
    invoice_id = Column(Integer)
    item_name = Column(String(100))
    specification = Column(String(100))
    unit = Column(String(20))
    quantity = Column(Integer)
    unit_price = Column(Float)
    amount = Column(Float)
    tax_rate = Column(Float)
    tax_amount = Column(Float)
    created_at = Column(DateTime)
    updated_at = Column(DateTime)
    #定义打印方法
    def __repr__(self):
        return (f"<InvoiceItem(id={self.id}, "
                f"invoice_id={self.invoice_id}, "
                f"item_name='{self.item_name}', "
                f"specification='{self.specification}', "
                f"unit='{self.unit}', "
                f"quantity={self.quantity}, "
                f"unit_price={self.unit_price}, "
                f"amount={self.amount}, "
                f"tax_rate={self.tax_rate}, "
                f"tax_amount={self.tax_amount}, "
                f"created_at={self.created_at}, "
                f"updated_at={self.updated_at})")



class DifyBindUser(Base):
    __tablename__ = 'dify_bind_user'
    '''
    id,主键,自增
    app_user_id,用户id
    dify_user_id,dify用户id
    '''
    id = Column(Integer, primary_key=True, autoincrement=True)
    app_user_id = Column(Integer)
    dify_user_id = Column(String(256))
    #定义打印方法
    def __repr__(self):
        return (f"<DifyBindUser(id={self.id}, "
                f"app_user_id={self.app_user_id}, "
                f"dify_user_id='{self.dify_user_id}'")
